package com.baizhi.controller;

import com.baizhi.entity.PictureSwitch;
import com.baizhi.entity.Result;
import com.baizhi.service.PictureSwitchService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("banner")
public class PictureSwitchController {

    @Autowired
    private PictureSwitchService pictureSwitchService;


    @RequestMapping("findAll")
    @ResponseBody
    public List<PictureSwitch> findAll(){
        List<PictureSwitch> all = pictureSwitchService.findAll();
        return all;
    }


    @RequestMapping("findById")
    @ResponseBody
    public PictureSwitch findById(String id){
        PictureSwitch byId = pictureSwitchService.findById(id);
        return byId;
    }

    @RequestMapping("save")
    @ResponseBody
    public Result save(MultipartFile pictureswitch, HttpServletRequest request) throws IOException {
        Result result = null;
        //获取绝对路径
        String realPath = request.getSession().getServletContext().getRealPath("/back/image/banner");
        PictureSwitch pictureSwitch = new PictureSwitch();
        try{
            String uuid = UUID.randomUUID().toString();
            pictureSwitch.setId(uuid);
            String extension = FilenameUtils.getExtension(pictureswitch.getOriginalFilename());
            String name=uuid+"."+extension;
            String path="/back/image/banner/"+name;
            pictureSwitch.setPath(path);
            pictureSwitch.setStatus(0);
            pictureswitch.transferTo(new File(realPath,name));
            pictureSwitchService.save(pictureSwitch);
            result = new Result();
            result.setStatus(true);
            result.setMessage("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("添加失败");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("update")
    public Result update(PictureSwitch pictureSwitch){
        Result result = null;
        try{
            pictureSwitchService.update(pictureSwitch);
            result = new Result();
            result.setStatus(true);
            result.setMessage("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("添加失败");
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(String id,HttpServletRequest request){
        PictureSwitch byId = pictureSwitchService.findById(id);
        System.out.println(byId.getPath()+"=======");
        String realPath = request.getSession().getServletContext().getRealPath(byId.getPath());
        File file = new File(realPath);
        file.delete();
        pictureSwitchService.delete(id);
        Result result = new Result();
        result.setStatus(true);
        return result;
    }
}
